package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.UserPref;
import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

@SuppressLint("ParserError")
public class CompClase extends Componente {

	private Paint _borderPaint;
	private Paint _textPaint;
	private Paint _focusPaint;
	private Paint _connectingPaint;
	
	
	
	private ArrayList<CompAtributo> _atributos = new ArrayList<CompAtributo>();
	private ArrayList<CompMetodo> _metodos = new ArrayList<CompMetodo>();

	public CompClase(String Titulo){
		if(UserPref.getInstancia().isUsingConvections() && Titulo.length() > 1){
			this.Titulo = Titulo.substring(0,1).toUpperCase() + Titulo.substring(1);
		}else{
			this.Titulo =  Titulo;
		}
		_centerPoint = new Point();
		_borderPaint = new Paint();
		_borderPaint.setColor(Color.BLACK);
		_borderPaint.setStrokeWidth(3);
		_borderPaint.setAntiAlias(false);
		_textPaint = new Paint();
		_textPaint.setColor(Color.BLACK);
		_textPaint.setTextSize(25);
		_focusPaint = new Paint();
		_focusPaint.setColor(Color.GREEN);
		_focusPaint.setStrokeWidth(4);
		_connectingPaint = new Paint();
		_connectingPaint.setColor(Color.YELLOW);
		_connectingPaint.setStrokeWidth(4);
		Point punto = new Point();
		punto.x = 20;
		punto.y = 20;
		this.setAnchor(punto);
		_width = _textPaint.measureText(Titulo)+15;
		_topAnchors = new BorderAnchors(4, Anchor, new Point((int)(Anchor.x+_width),Anchor.y));
		_bottomAnchors = new BorderAnchors(4,new Point(Anchor.x,(int)(Anchor.y+_height)) , new Point((int)(Anchor.x+_width),(int)(Anchor.y+_height)));
		_leftAnchors = new BorderAnchors(4, Anchor , new Point((int)(Anchor.x),(int)(Anchor.y+_height)));
		_rightAnchors = new BorderAnchors(4,new Point((int)(Anchor.x+_width),(int)(Anchor.y)) , new Point((int)(Anchor.x+_width),(int)(Anchor.y+_height)));
	}
	
	@Override
	public void Dibujar(Canvas c,boolean isFcoused,float globalScale,int offSetX,int offSetY) {
		int posX = Anchor.x + offSetX;
		int posY = Anchor.y + offSetY;
		_textPaint.setTextSize(25*globalScale);				
		//Dibujamos el Cuadro
		if(isFocus){
			c.drawLine(posX*globalScale,posY*globalScale,(posX+_width)*globalScale,posY*globalScale,_focusPaint);
			c.drawLine(posX*globalScale,posY*globalScale,posX*globalScale,(posY+_height)*globalScale,_focusPaint);
			c.drawLine(posX*globalScale,(posY+_height)*globalScale,(posX+_width)*globalScale,(posY+_height)*globalScale,_focusPaint);
			c.drawLine((posX+_width)*globalScale,posY*globalScale,(posX+_width)*globalScale,(posY+_height)*globalScale,_focusPaint);
		
		}else{
			c.drawLine(posX*globalScale,posY*globalScale,(posX+_width)*globalScale,posY*globalScale,_borderPaint);
			c.drawLine(posX*globalScale,posY*globalScale,posX*globalScale,(posY+_height)*globalScale,_borderPaint);
			c.drawLine(posX*globalScale,(posY+_height)*globalScale,(posX+_width)*globalScale,(posY+_height)*globalScale,_borderPaint);
			c.drawLine((posX+_width)*globalScale,posY*globalScale,(posX+_width)*globalScale,(posY+_height)*globalScale,_borderPaint);
		}
		
		//Dibujamos la linea del titulo
		c.drawLine(posX*globalScale,(posY+30)*globalScale,(posX+_width)*globalScale,(posY+30)*globalScale,_borderPaint);
		
		//Dibujamos el titulo
		c.drawText(Titulo, (posX+5)*globalScale ,(posY+25)*globalScale , _textPaint);
		
		//Dibujamos los atributos
		int puntoEscritura = 0;
		for (CompAtributo value : _atributos) {
			puntoEscritura += 30;
			c.drawText(value.getText(), (posX+5)*globalScale ,(posY+30+puntoEscritura)*globalScale , _textPaint);
		}
		//Dibujamos la linea q separa metodos y atributos
		c.drawLine(posX*globalScale,(posY+45+puntoEscritura)*globalScale,(posX+_width)*globalScale,(posY+45+puntoEscritura)*globalScale,_borderPaint);
		puntoEscritura += 10;
		
		//Dibujamos los metodos
		for (CompMetodo value : _metodos) {
			puntoEscritura += 30;
			c.drawText(value.getText(), (posX+5)*globalScale ,(posY+30+puntoEscritura)*globalScale , _textPaint);
		}
		
		//Dibujamos Los conectores Padre
		for (CompConector value : _conectorsBegining) {
			value.Dibujar(c, isFcoused, globalScale, offSetX, offSetY);
		}
		
	}

	@Override
	public boolean CalcularVisibilidad(int offSetX, int offSetY,float GlobalScale) {
		boolean Visible = true;
		if((Anchor.x + _width) <= offSetX){
			Visible = false;
		}else if(Anchor.x <= offSetX + UserPref.getInstancia().getWinWidth()){
			Visible = false;
		}else if(Anchor.y > offSetY + UserPref.getInstancia().getWinHeight()){
			Visible = false;
		}else if((Anchor.y + _height) <= offSetY){
			Visible = false;
		}
		return Visible;
	}

	@Override
	public boolean calcularTouch(Point touch) {
		boolean respuesta = false;
		if((touch.x <= Anchor.x+_width) && (touch.x >= Anchor.x)){
			if((touch.y <= Anchor.y+_height) && (touch.y >= Anchor.y)){
				isFocus = true;
				respuesta = true;
				//FactoryPopup.getPopup(VistaDiagrama.getVista(), "Clases");
			}
		}
		return respuesta;
	}

	public void addAtributo(CompAtributo nuevo){
		_atributos.add(nuevo);
		if(_width-10 < _textPaint.measureText(nuevo.getText()) ){
			_width = _textPaint.measureText(nuevo.getText())+10;
		}
		_height += 30;
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}
	
	public void calculoLongitud(){
		for (CompAtributo atr : _atributos) {
			if(_width-10 < _textPaint.measureText(atr.getText()) ){
				_width = _textPaint.measureText(atr.getText())+10;
			}
			_height += 30;
		}
		for (CompMetodo met : _metodos) {
			if(_width-10 < _textPaint.measureText(met.getText()) ){
				_width = _textPaint.measureText(met.getText())+10;
			}
			_height += 30;
		}		
	}	
	
	public ArrayList<CompAtributo> get_atributos() {
		return _atributos;
	}

	public void set_atributos(ArrayList<CompAtributo> _atributos) {
		this._atributos = _atributos;
	}

	public void addMetodo(CompMetodo nuevo){
		_metodos.add(nuevo);
		if(_width-10 < _textPaint.measureText(nuevo.getText()) ){
			_width = _textPaint.measureText(nuevo.getText())+10;
		}
		_height += 30;
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}

	@Override
	public void setTitulo(String titulo) {
		if(UserPref.getInstancia().isUsingConvections()){
			this.Titulo = titulo.substring(0,1).toUpperCase() + titulo.substring(1);
			if(_width-10 < _textPaint.measureText(Titulo) ){
				_width = _textPaint.measureText(Titulo)+10;
				
			}
		}else{
			this.Titulo =  titulo;
		}
		actualizarPuntoCentral();
	}

	@Override
	public boolean calcularTouchConectores(Point touch) {
		
		return false;
	}

	@Override
	public void actualizarPuntoCentral() {
			_centerPoint.set((Anchor.x+(int)(_width/2)), (Anchor.y+(int)(_height/2)));	
	}

	@Override
	public void actualizarAchorsPoints() {
		_topAnchors.actualizar(Anchor, new Point((int)(Anchor.x+_width),Anchor.y));
		_bottomAnchors.actualizar(new Point(Anchor.x,(int)(Anchor.y+_height)) , new Point((int)(Anchor.x+_width),(int)(Anchor.y+_height)));
		_leftAnchors.actualizar( Anchor , new Point((int)(Anchor.x),(int)(Anchor.y+_height)));
		_rightAnchors.actualizar(new Point((int)(Anchor.x+_width),(int)(Anchor.y)) , new Point((int)(Anchor.x+_width),(int)(Anchor.y+_height)));
	}

	public ArrayList<CompMetodo> get_metodos() {
		return _metodos;
	}

	public void set_metodos(ArrayList<CompMetodo> _metodos) {
		this._metodos = _metodos;
	}

	public CompMetodo getMetodoIndex(int index)
	{
		return this._metodos.get(index);
	}

}
